package dem1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-06-05
 * Time: 23:04
 */

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode head = lists[0];
        for(int i = 1; i < lists.length; i++){
            head = mergeKList(head,lists[i]);
        }
        return head;
    }
    private ListNode mergeKList(ListNode l1, ListNode l2){
        ListNode head = new ListNode();
        ListNode cur = head;
        while(l1 != null && l2 != null){
            int n1 = l1.val;
            int n2 = l2.val;
            if(n1 < n2){
                cur.next = l1;
                cur = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                cur = l2;
                l2 = l2.next;
            }
        }
        if(l1 != null){
            cur.next = l1;
        }else{
            cur.next = l2;
        }
        return head.next;
    }
    public ListNode create(int[] arr){
        ListNode head = new ListNode(arr[0]);
        ListNode cur = head;
        for(int i = 1; i < arr.length; i++){
            ListNode node = new ListNode(arr[i]);
            cur.next = node;
            cur = cur.next;
        }
        return head;
    }

    public static String reverseWords(String s) {
        Stack<Character> stack = new Stack<>();
        int length = s.length();
        int flag = 0;
        for(int i = 0; i < length; i++){
            char ch = s.charAt(i);
            if(ch != ' ' || flag == 1){
                stack.push(ch);
                if(ch == ' '){
                    flag = 0;
                }else{
                    flag = 1;
                }
            }else{
                flag = 0;//
            }
        }
        if(s.charAt(length - 1) == ' '){
            stack.pop();
        }
        char[] chs = new char[stack.size()];
        for(int i = 0; i < chs.length; i++){
            chs[i] = stack.pop();//
        }
        for(int i = 0; i < chs.length; i++){
            int j = i + 1;//
            while(j < chs.length && chs[j] != ' '){
                j++;
            }
            int n = j;
            j = j - 1;
            while(i < j){
                char tmp = chs[i];
                chs[i] = chs[j];
                chs[j] = tmp;
                i++;
                j--;
            }
            i = n;
        }
        String str = new String(chs);
        return str;
    }

    public static void main(String[] args) {
        String str = "  hello world  ";
        reverseWords(str);

//        Solution solution = new Solution();
//        ListNode[] lists = new ListNode[3];
//        int[] arr1 = {1, 4, 5};
//        int[] arr2 = {1, 3, 4};
//        int[] arr3 = {2, 6};
//        lists[0] = solution.create(arr1);
//        lists[1] = solution.create(arr2);
//        lists[2] = solution.create(arr3);
//        solution.mergeKLists(lists);
//        int[] arr = new int[0];
//        int len = arr.length;;
//        Map<Integer,String> map = new HashMap<>();
//        map.get(1);
//        ArrayList<Integer> arrayList = new ArrayList<>();
//        //arrayList.t
//        Stack<Character> stack = new Stack<>();
//        stack.p
    }
}
